#include "textbubblerenderer.h"
#include "map.h"
#include "mpoint.h"

TBuf8<100> 			TextBubbleRenderer::DEFAULT_TEXT;
float 				TextBubbleRenderer::BUBBLE_TEXT_SIZE = 16.0F;
float 				TextBubbleRenderer::TEXT_PADDING_LEFT = 10.0F;
float 				TextBubbleRenderer::TEXT_PADDING_TOP = 14.0F;
float 				TextBubbleRenderer::BOTTOM_SHADE = 8.0F;

TextBubbleRenderer::TextBubbleRenderer(const Context& context, TBool b)
	{
    if (Map.GetInstance().GetWidth() <= 320)
    	{
        BUBBLE_TEXT_SIZE = 18;
        TEXT_PADDING_LEFT =10.0F;
        TEXT_PADDING_TOP =11.0F;
    	}
    else
    	{
        BUBBLE_TEXT_SIZE = 20;
        TEXT_PADDING_LEFT =14.0F;
        TEXT_PADDING_TOP =13.0F;
    	}
    
    BOTTOM_SHADE =8.0F;
    bodyImg  = Tools::DecodeAssetDrawable(context, "bubble_body.png");
    footImg = Tools::DecodeAssetBitmap(context, "bubble_foot.png");
    bodyDownImg  = Tools::DecodeAssetDrawable(context, "bubble_body_down.png");
    footDownImg  = Tools::DecodeAssetBitmap(context, "bubble_foot_down.png");
    
    // 信息获取中
    DEFAULT_TEXT = _T("\u4FE1\u606F\u83B7\u53D6\u4E2D");
    
    this->context = &context;
	}
	
TextBubbleRenderer& TextBubbleRenderer::GetInstance(const Context& context)
	{
    if (instance == NULL)
    	instance = new TextBubbleRenderer(context, false);

    return instance;
	}
	
TextBubbleRenderer& TextBubbleRenderer::GetLittleInstance(const Context& context)
	{
    if (littleInstance == NULL)
      littleInstance = new TextBubbleRenderer(context, true);

    return littleInstance;
	}
	
const CFbsBitmap& TextBubbleRenderer::GetBodyImg(TBool b) const
	{
    if (b)
    	{
        return *bodyDownImg;
    	}
    else
    	{
        return *bodyImg;
    	}
	}

const CFbsBitmap& TextBubbleRenderer::GetFootImg(TBool b) const
	{
    if (b)
    	{
        return *footDownImg;
    	}
    else
    	{
        return *footImg;
    	}
	}
    
/**
 * 绘制泡泡
 * @param canvas
 * @param mPoint 泡泡在屏幕MPoint点坐标对象
 * @param paramBoolean true:事件down泡泡,false:事件up泡泡
 * @return 泡泡按钮矩形坐标
 */
RectF* TextBubbleRenderer::DrawBubble(CGraphicsProxy& canvas, MPoint* mPoint, TBool paramBoolean)
	{
    if (NULL == mPoint)
    	{
        return NULL;
    	}
    
//    this.mBubblePaint = new Paint();
//    this.mBubblePaint.setAntiAlias(true);
//    this.mBubblePaint.setFlags(1);
//    this.mBubblePaint.setTextSize(BUBBLE_TEXT_SIZE);
    const CFont& font = context->FontProvider().MakeFontL(BUBBLE_TEXT_SIZE);
    
    if (mPoint->GetName().Length() == 0){
        mPoint->SetName(DEFAULT_TEXT);            
    }
    
    TInt textWidth  = font.TextWidthInPixels(mPoint->GetName());
    const CFbsBitmap& footImg = GetFootImg(paramBoolean);
    const CFbsBitmap* bodyImg = GetBodyImg(paramBoolean);
    int bubbleWidth = (int) (textWidth + TEXT_PADDING_LEFT * 2);//TEXT_PADDING_LEFT * 2 左右间隔
    int bubbleHeight = (int) (BUBBLE_TEXT_SIZE + TEXT_PADDING_TOP * 2);//TEXT_PADDING_TOP * 2 上下间隔
    
   //泡泡按钮矩形坐标
    RectF* rf = new RectF();
    rf->left = mPoint->GetX() + mPoint->GetWidth()/2 -bubbleWidth/2;
    rf->top =  mPoint->GetY() - bubbleHeight - BOTTOM_SHADE/2;//mPoint.getY()：(点的图片)左上角的位置
    rf->right = rf->left + bubbleWidth; 
    rf->bottom = rf->top + bubbleHeight;
    
//        adjustMap(rf);
    
    //绘制泡泡按钮
    bodyImg.setBounds((int)rf.left, (int)rf.top, (int)rf.right, (int)rf.bottom);
    //bodyImg.draw(canvas);
    canvas.DrawImage();
    
    //绘制泡泡向下方向箭头
    float bX = rf->right - (rf->right - rf->left)/2 - footImg.SizeInPixels().iWidth/2;
    float bY = rf->bottom - BOTTOM_SHADE;
    canvas.DrawImage(TPoint(bX, bY), &footImg);
    
    //绘制文件(需要考虑文字长度)
    float tX = rf->left +TEXT_PADDING_LEFT;
    float tY = rf->top + bubbleHeight/2 + BUBBLE_TEXT_SIZE/2 - BOTTOM_SHADE/2;
    canvas.SetFont(font);
    canvas.DrawString(mPoint->GetName(), tX, tY);
    
    return rf;
	}
    
/**
 * 绘制泡泡
 * @param canvas
 * @param mPoint 泡泡在屏幕MPoint点坐标对象
 * @param hintStr 
 * @param paramBoolean true:事件down泡泡,false:事件up泡泡
 * @return 泡泡按钮矩形坐标
 */
RectF* TextBubbleRenderer::DrawBubble(CGraphicsProxy& canvas, MPoint* mPoint, const TDesC& hintStr, TBool paramBoolean)
	{
    if (NULL == mPoint){
        return NULL;
    }
//    this.mBubblePaint = new Paint();
//    this.mBubblePaint.setAntiAlias(true);
//    this.mBubblePaint.setFlags(1);
//    this.mBubblePaint.setTextSize(BUBBLE_TEXT_SIZE);
    const CFont& font = context->FontProvider().MakeFontL(BUBBLE_TEXT_SIZE);

    if (mPoint->GetName().Length() == 0){
        mPoint->SetName(DEFAULT_TEXT);            
    }
    float hintStrWidth = ((hintStr.Length() > 0)? font.TextWidthInPixels(hintStr):-1);
    float textWidth  = font.TextWidthInPixels(mPoint->GetName());
    if (hintStrWidth > textWidth){
    	textWidth = hintStrWidth;
    }
    const CFbsBitmap& footImg = GetFootImg(paramBoolean);
    const CFbsBitmap& bodyImg = GetBodyImg(paramBoolean);
    int bubbleWidth = (int) (textWidth + TEXT_PADDING_LEFT * 2);//TEXT_PADDING_LEFT * 2 左右间隔
    int bubbleHeight = (int) (BUBBLE_TEXT_SIZE * 2+ TEXT_PADDING_TOP * 2);//TEXT_PADDING_TOP * 2 上下间隔 ,；BUBBLE_TEXT_SIZE * 2:两行文字
    
   //泡泡按钮矩形坐标
    RectF* rf = new RectF();
    rf->left = mPoint->GetX() + mPoint->GetWidth()/2 -bubbleWidth/2;
    rf->top =  mPoint->GetY() - bubbleHeight - BOTTOM_SHADE/2;//mPoint.getY()：(点的图片)左上角的位置
    rf->right = rf->left + bubbleWidth; 
    rf->bottom = rf->top + bubbleHeight;
    
    //绘制泡泡按钮
    bodyImg.setBounds((int)rf.left, (int)rf.top, (int)rf.right, (int)rf.bottom);
    bodyImg.draw(canvas);
    
    //绘制泡泡向下方向箭头
    float bX = rf->right - (rf->right - rf->left)/2 - footImg.SizeInPixels().iWidth/2;
    float bY = rf->bottom - BOTTOM_SHADE -3;
    canvas.DrawImage(TPoint(bX, bY), &footImg);
    
    //绘制文件(需要考虑文字长度)
    float tX = rf->left +TEXT_PADDING_LEFT;
    float tY = rf->top + TEXT_PADDING_TOP + BUBBLE_TEXT_SIZE/2;
    
    canvas.SetFont(font);
    canvas.DrawString(mPoint->GetName(), tX, tY);
    
    canvas.SetPenColor(KRgbGray);
    tY = rf->top + TEXT_PADDING_TOP * 3 + BUBBLE_TEXT_SIZE/2;
    canvas.DrawString(hintStr, tX, tY);
    
	return rf;
	}
